package se.perp.c4.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import se.perp.c4.model.Card;

public class BattleResultDialog extends JDialog implements MouseListener {

    public static final long DEFAULT_TIMEOUT = 5000;
    private Card attackerCard,  defenderCard;
    private ImagePanel attackerPanel,  defenderPanel;
    private int dialogWidth = 500;
    private int dialogHeight;
    private String message;
    private final int TOP_PANEL_HEIGHT = 120;
    private final int BOTTOM_PANEL_HEIGHT = 50;
    private ImagePanel attackerCardImagePanel;
    private ImagePanel defenderCardImagePanel;
    private int attackerCardX;
    private int defenderCardX;
    private JPanel mainPanel;
    private Image attackerPortrait,  defenderPortrait;

    private class TopPanel extends JPanel {

        private Font vsFont;

        public TopPanel() {
            vsFont = FontSet.BATTLERESULT_VS;

            setBackground(ColorSet.BOARD_BG);
            JPanel subPanel = new JPanel(new GridLayout(1, 3));
            setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
            setPreferredSize(new Dimension(-1, TOP_PANEL_HEIGHT));
            subPanel.setBackground(null);
            JLabel vsLabel = new JLabel("vs.", SwingConstants.CENTER);
            vsLabel.setForeground(Color.WHITE);
            vsLabel.setFont(vsFont);

            attackerPanel = new ImagePanel(attackerPortrait);
            defenderPanel = new ImagePanel(defenderPortrait);
            subPanel.add(attackerPanel);
            subPanel.add(vsLabel);
            subPanel.add(defenderPanel);
            add(subPanel);
        }
    }

    private class CenterPanel extends JPanel {

        public CenterPanel(MouseListener listener) {
            setBackground(null);
            setLayout(null);
            if (attackerCardImagePanel != null && defenderCardImagePanel != null) {
                attackerCardImagePanel.setLocation(attackerCardX, 10);
                attackerCardImagePanel.addMouseListener(listener);

                defenderCardImagePanel.setLocation(defenderCardX, 10);
                defenderCardImagePanel.addMouseListener(listener);

                add(attackerCardImagePanel);
                add(defenderCardImagePanel);
            }
        }
    }

    private class BottomPanel extends JPanel {

        private Font statusFont = new Font(Font.SERIF, Font.BOLD, 24);

        public BottomPanel() {
            setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
            setBackground(ColorSet.BATTLE_DIALOG_MESSAGE_BG);
            setPreferredSize(new Dimension(-1, BOTTOM_PANEL_HEIGHT));
            setLayout(new BorderLayout());
            JPanel subPanel = new JPanel(new FlowLayout());
            subPanel.setBackground(null);
            JLabel messageLabel = new JLabel(message);
            messageLabel.setFont(statusFont);
            messageLabel.setForeground(Color.WHITE);
            subPanel.add(messageLabel);
            add(subPanel, BorderLayout.CENTER);
        }
    }

    public BattleResultDialog(
            final Frame owner,
            final Card attackerCard,
            final Card defenderCard,
            final Image attackerPortrait,
            final Image defenderPortrait,
            final String message,
            final long timeoutMillis) {

        super(owner);
        if (owner == null) {
            throw new IllegalArgumentException();
        }

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setModalityType(DEFAULT_MODALITY_TYPE);
        setUndecorated(true);

        this.attackerCard = attackerCard;
        this.defenderCard = defenderCard;
        this.attackerPortrait = attackerPortrait;
        this.defenderPortrait = defenderPortrait;
        if (attackerCard != null && defenderCard != null) {
            attackerCardImagePanel = new CardImagePanel(attackerCard);
            attackerCardImagePanel.setBackground(ColorSet.BOARD_BG);
            defenderCardImagePanel = new CardImagePanel(defenderCard);
            defenderCardImagePanel.setBackground(ColorSet.BOARD_BG);
        }

        this.message = message;

        dialogHeight = TOP_PANEL_HEIGHT + BOTTOM_PANEL_HEIGHT + 20;
        if (attackerCardImagePanel != null) {
            dialogHeight += attackerCardImagePanel.getHeight();
        }
        if (attackerCard != null) {
            attackerCardX = dialogWidth / 2 - 40 - attackerCardImagePanel.getWidth();
            defenderCardX = dialogWidth / 2 + 40;
        }

        mainPanel = new JPanel(new BorderLayout());
        setSize(dialogWidth, dialogHeight);

        mainPanel.setBackground(ColorSet.BOARD_BG);
        mainPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        mainPanel.add(new TopPanel(), BorderLayout.NORTH);
        mainPanel.add(new CenterPanel(this), BorderLayout.CENTER);
        mainPanel.add(new BottomPanel(), BorderLayout.SOUTH);

        add(mainPanel);
        mainPanel.addMouseListener(this);

        setLocation((owner.getWidth() - dialogWidth) / 2,
                (owner.getHeight() - dialogHeight) / 2);
        setLocationRelativeTo(owner);
        setVisible(true);

        TimerTask tt = new TimerTask() {

            @Override
            public void run() {
                dispose();
            }
        };

        Timer t = new Timer("BattleResultHider");
        t.schedule(tt, timeoutMillis);

    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        setVisible(false);
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}
